package com.stone.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.stone.bean.dto.ConfigAddDTO;
import com.stone.bean.dto.ConfigUpdateDTO;
import com.stone.config.SpringContextConfig;
import com.stone.config.exception.BusinessException;
import com.stone.entity.SysConfig;
import com.stone.mapper.SysConfigMapper;
import com.stone.service.SysConfigService;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 参数管理 服务实现类
 * </p>
 *
 * @author stone
 * @since 2022-04-02
 */
@Service
public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig> implements SysConfigService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        SysConfig sysConfig = this.baseMapper.selectById(id);
        if (Objects.isNull(sysConfig)) {
            throw new BusinessException("不存在的数据");
        }
        // 级联删除 子级
        List<SysConfig> list = this.baseMapper.selectList(new QueryWrapper<SysConfig>().eq("parent_code", sysConfig.getConfigCode()));
        if (CollUtil.isNotEmpty(list)) {
            List<Long> collect = list.stream().map(SysConfig::getId).collect(Collectors.toList());
            this.baseMapper.deleteBatchIds(collect);
        }
        this.baseMapper.deleteById(id);
    }

    @Override
    public void add(ConfigAddDTO dto, String token) {
        SysConfig sysConfig = JSON.parseObject(JSON.toJSONString(dto), SysConfig.class);
        sysConfig.setEnable(true);
        sysConfig.setCreateBy(SpringContextConfig.loginUserId(token));
        sysConfig.setUpdateBy(SpringContextConfig.loginUserId(token));
        sysConfig.setCreateTime(LocalDateTime.now());
        sysConfig.setUpdateTime(LocalDateTime.now());
        try {
            save(sysConfig);
        } catch (DuplicateKeyException e) {
            throw new BusinessException("参数编码已存在");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ConfigUpdateDTO dto, String token) {
        SysConfig sysConfig = JSON.parseObject(JSON.toJSONString(dto), SysConfig.class);
        SysConfig db = getById(dto.getId());
        if (Objects.isNull(db)) {
            throw new BusinessException("不存在的数据");
        }
        sysConfig.setUpdateBy(SpringContextConfig.loginUserId(token));
        sysConfig.setUpdateTime(LocalDateTime.now());
        try {
            updateById(sysConfig);
        } catch (DuplicateKeyException e) {
            throw new BusinessException("参数编码已存在");
        }
        if (!db.getConfigCode().equalsIgnoreCase(sysConfig.getConfigCode())) {
            // 级联更新，更新子级的parentCode
            this.baseMapper.updateParentCode(db.getConfigCode(), sysConfig.getConfigCode());
        }
    }
}
